import Vue from 'vue'
import Vuex from 'vuex'

import $mConfig from '@/config/index.config.js'
import $mRoutesConfig from '@/config/routes.config.js'
import $mRouter from '@/common/router.js'
import $mUtils from '@/common/utils.js'
import * as $apis from '@/apis/index.js'

// #ifdef H5  
import $wechat from '@/common/wechat.js'  
// #endif
  
Vue.use(Vuex)
const TOKEN = uni.getStorageSync("token") || "";
const OPENID = uni.getStorageSync("openId") || "";
const USER_INFO = uni.getStorageSync("userInfo") || {
				id: null,
				mobile: null,
				nickname: null,
				portrait: null
		};
const REFERENCE_ID = uni.getStorageSync("referenceId") || null;
const EXPIRE_TIME = uni.getStorageSync("EXPIRE_TIME") || 0;
const VERSION = uni.getStorageSync("version") || "0";

const IS_SILENT_LOGIN = uni.getStorageSync("isSilentLogin");

const store = new Vuex.Store({
	state: {
		// 是否強制登录
		forcedLogin:$mConfig.forcedLogin,
		// 前端token
		token: TOKEN,
		expireTime: EXPIRE_TIME,
		// 用户openid
		openId: OPENID,
		userInfo : USER_INFO,
		goodsMsgCache:null,
		referenceId :REFERENCE_ID,
		music:null,
		isPlaying:false,
		hasCanPlay:false,
		selectAddress:null,
		preludeMusic:null,
		isPreludeMusicFinished:false,
		hasShareReaded:false,
		orderId:null,
		version:VERSION,
		isSilentLogin:IS_SILENT_LOGIN,//静默登录
		// #ifdef H5  
		isWechat:$wechat.isWechat(),
		// #endif  
	},
	getters: {
		// 用户是否登录
		hasLogin: state => {
			var d = new Date();
			console.log(d.getTime() + "==================" + state.expireTime)
			if (state.token && state.token != -99 && d.getTime() < state.expireTime) {
				console.log("===========login============true========")
				return true;
			} else {
				return false
			}
		},
	},
	mutations: {
		SET_VERSION(state, version) {
			state.version = version;
		},
		SET_IS_SILENT_LOGIN(state, isSilentLogin) {
			state.isSilentLogin = isSilentLogin;
			uni.setStorageSync("isSilentLogin", isSilentLogin);
		},
		SET_ORDER_ID(state, orderId) {
			state.orderId = orderId;
		},
		SET_HAS_SHARE_READED(state, hasShareReaded) {
			state.hasShareReaded = hasShareReaded;
		},
		SET_IS_PRELUDE_MUSIC_FINISHED(state, isPreludeMusicFinished) {
			state.isPreludeMusicFinished = isPreludeMusicFinished;
		},
		SET_EXPIRE_TIME(state, expireTime) {
			state.expireTime = expireTime;
			if(expireTime != 0){
				uni.setStorageSync("EXPIRE_TIME", expireTime);
			}else{
				uni.removeStorageSync("EXPIRE_TIME");
			}
		},
		SET_SELECT_ADDRESS(state, selectAddress) {
			state.selectAddress = selectAddress;
		},
		SET_CAN_PLAY(state, hasCanPlay) {
			state.hasCanPlay = hasCanPlay;
		},
		SET_PLAYING(state, isPlaying) {
			state.isPlaying = isPlaying;
		},
		SET_GOODS_MSG_CACHE(state, goodsMsgCache) {
			state.goodsMsgCache = goodsMsgCache;
		},
		SET_PRELUDE_MUSIC(state, preludeMusic) {
			state.preludeMusic = preludeMusic;
		},
		SET_MUSIC(state, music) {
			state.music = music;
		},
		SET_TOKEN(state, token) {
			state.token = token;
			state.goodsMsgCache = null;
			uni.setStorageSync("token", token);
		},
		SET_OPENID(state, openId) {
			state.openId = openId;
			uni.setStorageSync("openId", openId);
		},
		SET_USERINFO(state, userInfo) {
			state.userInfo = userInfo;
			uni.setStorageSync("userInfo", userInfo);
		},
		SET_LOGOUT(state) {
			state.token = "";
			uni.setStorageSync("token", "");
		},
		SET_REFERENCE_ID(state, referenceId) {
			state.referenceId = referenceId;
			uni.setStorageSync("referenceId", referenceId);
		},
	},
	actions: {
		// 登录过期 重新登录
		reLogin({
			commit
		}, info) {
			commit("SET_TOKEN","");
			// 登录成功后的重定向地址和参数
			// 没有登录 是否强制登录?
				var oldUrl = $mUtils.objParseUrlAndParam($mRoutesConfig.login.path, {})
				// #ifdef H5  
				if($wechat.isWechat()){
					oldUrl = $mUtils.objParseUrlAndParam($mRoutesConfig.wxh5SilentLogin.path, {});
				}
				// #endif  

				// #ifdef MP
				oldUrl = $mUtils.objParseUrlAndParam($mRoutesConfig.wxXcxlogin.path, {})
				// #endif  
				if ($mConfig.forcedLogin) {
					uni.redirectTo({
						url: oldUrl
					})
				} else {
					uni.navigateTo({
						url: oldUrl
					})
				}
			//$mRouter.redirectTo({
			//	route:$mRoutesConfig.login
			//});
		},
	}
})

export default store
